package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.DateTimeAdapter;
import com.xianbeiying.app.ckgl.bean.DateTimeBean;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.GetDateTime;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.util.DialogUtile;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.dapengView.DpView;
import com.xianbeiying.app.ckgl.widght.dapengView.Dpdata;
import com.xianbeiying.app.ckgl.widght.datepop.NumberPickerView;
import com.xianbeiying.app.ckgl.widght.datetimepicker.date.DatePickerDialog;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 时间和日期
 */
public class DateTimeActivity extends BaseActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, HttpCallBackUtil, Dpdata {
    private ImageView iv_title_left;
    private TextView tv_title;
    private TextView tv_right_title;
    //自动
    private TextView tv_auto_time;
    private ImageView iv_auto_time;
    private View view_line1;
    private RelativeLayout rl_auto_date;
    //手动
    private TextView tv_multe_time, tv_multe_date;
    private ImageView iv_multe_time;
    private View view_line2, view_line3;
    private RelativeLayout rl_mul_date, rl_mul_time;
    private GetModel getModel;
    private SetModel setModel;
    //水平滑动的
    private LinearLayout ly_wphorizontal;  //水平滑动的布局
    private View va_line;
    private TextView tv_va_all;
    private DpView dpView;
    private ListView date_time_list;
    private DateTimeAdapter dateTimeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_time);
        initView();
    }

    private void initView() {
        MainActivity.fragmentid = 3;
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        setModel = new SetModel(this);
        setModel.addResponseListener(this);
        date_time_list = (ListView) findViewById(R.id.date_time_list);
        ly_wphorizontal = (LinearLayout) findViewById(R.id.ly_wphorizontal);
        va_line = (View) findViewById(R.id.va_line);
        tv_va_all = (TextView) findViewById(R.id.tv_va_all);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_right_title = (TextView) findViewById(R.id.tv_right_title);
        //自动
        tv_auto_time = (TextView) findViewById(R.id.tv_auto_time);
        iv_auto_time = (ImageView) findViewById(R.id.iv_auto_time);
        view_line1 = (View) findViewById(R.id.view_line1);
        rl_auto_date = (RelativeLayout) findViewById(R.id.rl_auto_date);
        //手动
        tv_multe_time = (TextView) findViewById(R.id.tv_multe_time);
        tv_multe_date = (TextView) findViewById(R.id.tv_multe_date);
        iv_multe_time = (ImageView) findViewById(R.id.iv_multe_time);
        view_line2 = (View) findViewById(R.id.view_line2);
        view_line3 = (View) findViewById(R.id.view_line3);
        rl_mul_date = (RelativeLayout) findViewById(R.id.rl_mul_date);
        rl_mul_time = (RelativeLayout) findViewById(R.id.rl_mul_time);
        tv_title.setText("时间和日期");
        iv_title_left.setOnClickListener(this);
        iv_auto_time.setOnClickListener(this);
        iv_multe_time.setOnClickListener(this);
        rl_mul_date.setOnClickListener(this);
        rl_mul_time.setOnClickListener(this);
        tv_right_title.setOnClickListener(this);
        initData();
    }

    private void getData(List<DevicesBean.GREENHOUSEBean> DEVICES, int size, final int id) {
        dpView = new DpView(DEVICES, size, id, ly_wphorizontal, this);
        dpView.setDpdata();
        dpView.addResponseListener(this);
    }

    private List<String> year, month;
    private int yearId, monthId;
    //网络返回值
    private DevicesBean devicesBean;

    private void initData() {
        year = com.xianbeiying.app.ckgl.util.DataUtil.getTimeHHs();
        yearId = com.xianbeiying.app.ckgl.util.DataUtil.timeHHid;
        month = com.xianbeiying.app.ckgl.util.DataUtil.getTimeMMs();
        monthId = com.xianbeiying.app.ckgl.util.DataUtil.timeMMid;
//        if (SharedPreferencesUtils.getBooleanDate("zidong")) {
//            //显示手动
//            showDefault();
//            iv_multe_time.setSelected(true);
//            rl_mul_date.setVisibility(View.VISIBLE);
//            rl_mul_time.setVisibility(View.VISIBLE);
//            view_line2.setVisibility(View.VISIBLE);
//            view_line3.setVisibility(View.VISIBLE);
//            tv_right_title.setText("确定");
//            tv_multe_date.setText(DataUtil.dateTime());
//            tv_multe_time.setText(DataUtil.tiemStr());
//        } else {
//            //显示自动
//            showDefault();
//            tv_right_title.setText("");
//            iv_auto_time.setSelected(true);
//            rl_auto_date.setVisibility(View.VISIBLE);
//            view_line1.setVisibility(View.VISIBLE);
//            tv_auto_time.setText(DataUtil.dateTime() + "  " + DataUtil.tiemStr());
//        }
        if (MyApplication.devicesBean != null) {
            LogUtils.e("---------进入路");
            devicesBean = MyApplication.devicesBean;
            getData(devicesBean.getGREENHOUSE(), devicesBean.getGREENHOUSE().size(), 0);
            getModel.GET_DATE("GET_DATE", devicesBean.getGREENHOUSE().get(0).getGreenhouseId(), true);
        } else {
            //获取大棚数据
            getModel.QUWEYDEVS(true);

        }
    }

    private int selectIds;
    private CenterPopWindow selectTime;
    private NumberPickerView mPickerViewH;
    private NumberPickerView mPickerViewM;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            //确定
            case R.id.tv_right_title:
                String time;
                String date;
                if (selectIds == 2) {
                    time = tv_multe_time.getText().toString().trim();
                    date = tv_multe_date.getText().toString().trim();
                    setModel.SET_DATE("SET_DATE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), date + " " + time, true);
                }

                break;
            //自动
            case R.id.iv_auto_time:
                showDefault();
                selectIds = 1;
                tv_right_title.setText("");
                SharedPreferencesUtils.setBooleanDate("zidong", false);
                iv_auto_time.setSelected(true);
                rl_auto_date.setVisibility(View.VISIBLE);
                view_line1.setVisibility(View.VISIBLE);
                DialogUtile.showLoading(this);
                new Handler().postDelayed(new Runnable(){
                    public void run() {
                        try{
                            DialogUtile.closeDialog();
//                            String dates=DataUtil.getWebsiteDatetime("http://www.baidu.com");
                            tv_auto_time.setText(DataUtil.dateTime() + "  " + DataUtil.tiemStr());
                            String time = tv_auto_time.getText().toString().trim();
                            setModel.SET_DATE("SET_DATE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), time, true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },3000);
                break;
            //手动
            case R.id.iv_multe_time:
                SharedPreferencesUtils.setBooleanDate("zidong", true);
                showDefault();
                selectIds = 2;
                tv_right_title.setText("确定");
                iv_multe_time.setSelected(true);
                rl_mul_date.setVisibility(View.VISIBLE);
                rl_mul_time.setVisibility(View.VISIBLE);
                view_line2.setVisibility(View.VISIBLE);
                view_line3.setVisibility(View.VISIBLE);
                break;
            //选择日期
            case R.id.rl_mul_date:
                rl_mul_date.setTag("1");
                getTime();
                break;
            //时//分
            case R.id.rl_mul_time:
                selectTime=new CenterPopWindow(DateTimeActivity.this,R.layout.pop_window_numbertime);
                mPickerViewH =selectTime.getView(R.id.picker_hour);
                mPickerViewM =selectTime.getView(R.id.picker_minute);
                initTime();
                selectTime.setTextOnClickListener(R.id.done,this);
                selectTime.setTextOnClickListener(R.id.ly_pop_time,this);
                break;
            //时间的确认
            case R.id.ly_pop_time:
                selectTime.dismiss();
                break;
            case R.id.done:
                tv_multe_time.setText(mPickerViewH.getContentByCurrValue()+":"+mPickerViewM.getContentByCurrValue());
                selectTime.dismiss();
                break;
        }
    }

    private void showDefault() {
        iv_auto_time.setSelected(false);
        iv_multe_time.setSelected(false);
        view_line1.setVisibility(View.GONE);
        view_line2.setVisibility(View.GONE);
        view_line3.setVisibility(View.GONE);
        rl_auto_date.setVisibility(View.GONE);
        rl_mul_date.setVisibility(View.GONE);
        rl_mul_time.setVisibility(View.GONE);
    }

    private void getTime() {
        final Calendar calendar = Calendar.getInstance();
        final DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), true);
        datePickerDialog.setYearRange(1985, 2028);
        datePickerDialog.setCloseOnSingleTapDay(true);
        datePickerDialog.show(getSupportFragmentManager(), "");
    }

    @Override
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
        if (rl_mul_date.getTag() != null && rl_mul_date.getTag().equals("1")) {
            tv_multe_date.setText(year + "-" + (month + 1) + "-" + day);
        }
//        if(rl_mul_time.getTag()!=null&&rl_mul_time.getTag().equals("2")){
//            tv_multe_time.setText(year+"-"+(month+1)+"-"+day);
//        }
    }

    private GetDateTime getDateTime;

    private DateTimeBean dateTimeBean;

    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.QUWEYDEVS)) {
            if (content != null) {
                devicesBean = JSON.parseObject(new String((byte[]) content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                getData(devicesBean.getGREENHOUSE(), devicesBean.getGREENHOUSE().size(), 0);
                getModel.GET_DATE("GET_DATE", devicesBean.getGREENHOUSE().get(0).getGreenhouseId(), true);
            }
        }
        if (url.equals("获取时间")) {
            getDateTime = JSON.parseObject(new String((byte[]) content), GetDateTime.class);
            if (getDateTime.isR()) {
                dateTimeAdapter = new DateTimeAdapter(this, getDateTime);
                date_time_list.setAdapter(dateTimeAdapter);
            } else {
                ToastUtil.showInfo(DateTimeActivity.this, getDateTime.getERR_MSG());
            }

        }

        if (url.equals("时间校验")) {
            dateTimeBean = JSON.parseObject(new String((byte[]) content), DateTimeBean.class);
            if (dateTimeBean.isR()) {
                ToastUtil.showInfo(DateTimeActivity.this, "修改成功");
                getModel.GET_DATE("GET_DATE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), false);
            } else {
                ToastUtil.showInfo(DateTimeActivity.this, dateTimeBean.getERR_MSG());
            }
        }
    }

    //时间的设置
    private void initTime(){
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        h = h % 24;
        setData(mPickerViewH, 0, 23, h);
        setData(mPickerViewM, 0, 59, m);
    }

    private void setData(NumberPickerView picker, int minValue, int maxValue, int value){
        picker.setMinValue(minValue);
        picker.setMaxValue(maxValue);
        picker.setValue(value);
    }
    private int faId;
    @Override
    public void onDpdata(int sid, boolean isChoice){
        if (isChoice) {
            faId = sid;
            showDefault();
            getModel.GET_DATE("GET_DATE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), true);
        }
    }
}
